package com.xiaoyi.analyzer;

import com.xiaoyi.equery.EQueryModel;
import com.xiaoyi.equery.QueryResultModel;
import com.xiaoyi.analyzer.group.CommonGroup;
import java.util.Map;
import java.util.Objects;

/**
 * 普通分组解析器
 * @author xiaoyi
 * @version 1.0.0
 * @createTime 2023-02-19 22:27:00
 */
public class NormalAggregateDataAnalyzer extends  AbstractDataAnalyzer{
    @Override
    public Object doAnalysis(EQueryModel commonConfig, QueryResultModel queryResultModel) {
        String aggField = commonConfig.getAggField();
        String[] split = aggField.split(",");
        CommonGroup commonGroup=null;
        for (int i = split.length-1; i >=0 ; i--) {
            CommonGroup commonGroupTemp = new CommonGroup(split[i]);
            commonGroupTemp.setCommonGroup(commonGroup);
            commonGroup = commonGroupTemp;
        }
        if (Objects.isNull(commonGroup)){
            return queryResultModel.getQueryResultList();
        }
        Map<String, Object> group = commonGroup.group(queryResultModel.getQueryResultList());
        return group;
    }
}
